<template>
  <div v-loading="createLoading">
    <div class="base_header" style="text-align: left;margin-bottom: 20px">
      <span class="left-title">
        发布公告
      </span>
      <span style="font-size: 10px">所有用户都会接受到该条公告</span>
      <el-button type="success"
                 style="float: right"
                 @click="createAnnounce"
      >发布</el-button>
    </div>
    <div class="edit_div">
      <el-form v-model="notifyData" label-position="left" label-width="80px">
        <el-form-item label="主题">
          <el-input type="text" v-model="notifyData.title"></el-input>
        </el-form-item>
        <el-form-item label="内容">
          <div style="height: 500px;width: 100%">
            <Editor type="webImage" ref="editor" @getContent="getContent"></Editor>
          </div>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import Editor from '../../common/Editor'
import {mapState} from 'vuex'
export default {
  name: 'CreateAnnounce',
  data () {
    return {
      createLoading: false,
      notifyData: {
        title: '',
        content: '',
        type: 1,
        sender: ''
      }
    }
  },
  computed: mapState({
    user: 'user'
  }),
  components: {
    Editor
  },
  methods: {
    createAnnounce () {
      this.createLoading = true
      this.notifyData.sender = this.user.userId
      var _this = this
      this.$axios.post('/api/notify/addAnnounce',
        this.$qs.stringify({
          notifyData: JSON.stringify(this.notifyData)
        })
      )
        .then(function (response) {
          var data = response.data
          _this.createLoading = false
          if (data.isSuccess) {
            _this.$message({
              type: 'success',
              message: '成功',
              offset: 50
            })
            _this.notifyData.title = ''
            _this.$refs.editor.setContent('')
          } else {
            _this.$message({
              type: 'error',
              message: '发布失败，请重试！',
              offset: 50
            })
          }
        })
        .catch(function (e) {
          _this.$message({
            type: 'error',
            message: '系统出错，请重试！',
            offset: 50
          })
          _this.createLoading = false
        })
    },
    getContent (value) {
      this.notifyData.content = value
    }
  }
}
</script>

<style>
  .edit_div label{
    font-weight: bolder;
  }
  .base_header .left-title{
     font-size: 30px;
     font-weight: normal;
   }
</style>
